package org.sundy.webservice.cxf.demo;

public class CXFFactory {

	private Class serviceClass;

	private String wsdlURL;

	private String namespaceURL, localPart;

	private String delegateMethod = "getPort";

	public Object getService() {
		try {
			if (null == wsdlURL) {
				return serviceClass.getConstructor().newInstance();
			}
			if (null == namespaceURL) {
				return serviceClass.getConstructor(java.net.URL.class)
						.newInstance(new java.net.URL(wsdlURL));
			}
			java.lang.reflect.Constructor ct = serviceClass.getConstructor(
					java.net.URL.class, javax.xml.namespace.QName.class);
			if (null == localPart) {
				return ct.newInstance(new java.net.URL(wsdlURL),
						new javax.xml.namespace.QName(namespaceURL));
			}
			return ct.newInstance(new java.net.URL(wsdlURL),
					new javax.xml.namespace.QName(namespaceURL, localPart));
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public Object createDelegate() {
		java.lang.reflect.Method m;
		try {
			m = serviceClass.getMethod(delegateMethod, null);
			Object delegate = m.invoke(getService(), null);
			return delegate;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public Class getServiceClass() {
		return serviceClass;
	}

	public void setServiceClass(Class serviceClass) {
		this.serviceClass = serviceClass;
	}

	public String getWsdlURL() {
		return wsdlURL;
	}

	public void setWsdlURL(String wsdlURL) {
		this.wsdlURL = wsdlURL;
	}

	public String getNamespaceURL() {
		return namespaceURL;
	}

	public void setNamespaceURL(String namespaceURL) {
		this.namespaceURL = namespaceURL;
	}

	public String getLocalPart() {
		return localPart;
	}

	public void setLocalPart(String localPart) {
		this.localPart = localPart;
	}

	public String getDelegateMethod() {
		return delegateMethod;
	}

	public void setDelegateMethod(String delegateMethod) {
		this.delegateMethod = delegateMethod;
	}

}
